# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Yuui Tanabe <yuuitanabe@163.com>

_realname=gnupg
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.8
pkgrel=1
pkgdesc="GNU Privacy Guard - a PGP replacement tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libksba"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libassuan"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libusb-compat"
         "${MINGW_PACKAGE_PREFIX}-npth"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-pinentry")
makedepends=("texinfo" "${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
# REMOVE openldap package before build
license=('GPL')
options=(emptydirs)
url="https://gnupg.org/"
source=(https://gnupg.org/ftp/gcrypt/gnupg/${_realname}-${pkgver}.tar.bz2{,.sig}
        01-mingw32-fix-potomo.mingw.patch
        02-mingw-sysconfigdir-under-prefix.patch
        03-ldap-libraries-check-order.patch
        04-dos-extra-defs.patch)
sha256sums=('540b7a40e57da261fb10ef521a282e0021532a80fd023e75fb71757e8a4969ed'
            'SKIP'
            '592396d968353f991c879488b4b3e55a550873fa74703e08c7d28bb22241d8b9'
            'a936d17a8dc60fb401db07d040e55e03ba382d5e38871d1b62f527b1aa458dc1'
            'b985b01edb3924b62ed99180f0b08a6ae2cdcab0fc81d14cd7169ccb278e756e'
            '82b0cccc99d7303d05fcea4d7295f63b64fd92f7ea346312bfc4a69794237920')
validpgpkeys=(
  '5B80C5754298F0CB55D8ED6ABCEF7E294B092E28' # Andre Heinecke (Release Signing Key)
  '6DAA6E64A76D2840571B4902528897B826403ADA' # Werner Koch (dist signing 2020)
  'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD' # Niibe Yutaka (GnuPG Release Key)
  '02F38DFF731FF97CB039A1DA549E695E905BA208' # GnuPG.com (Release Signing Key 2021)
)

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/01-mingw32-fix-potomo.mingw.patch
  patch -p1 -i ${srcdir}/02-mingw-sysconfigdir-under-prefix.patch
  patch -p1 -i ${srcdir}/03-ldap-libraries-check-order.patch
  patch -p1 -i ${srcdir}/04-dos-extra-defs.patch

  autoreconf -fiv
  sed 's/development_version=yes/development_version=no/' -i configure
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

# I had to disable doc generation because that would cause a
#a segmentation fault with something related to tex files.
#It would only happen for 32-bit versions.
  CFLAGS+=" -Wno-int-conversion" \
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --libexecdir=${MINGW_PREFIX}/bin \
      --sbindir=${MINGW_PREFIX}/bin \
      --sysconfdir=${MINGW_PREFIX}/etc/gnupg \
      --enable-maintainer-mode \
      --enable-gpgtar \
      --disable-doc \
      --disable-g13
  
  MSYS2_ARG_CONF_EXCL="-DGNUPG_BINDIR=;-DGNUPG_LIBEXECDIR=;-DGNUPG_LIBDIR=;-DGNUPG_DATADIR=;-DGNUPG_SYSCONFDIR=;-DGNUPG_LOCALSTATEDIR=;-DLOCALEDIR=" \
  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  # a few tests fail because of not executable errors
  make check || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  MSYS2_ARG_CONF_EXCL="-DGNUPG_BINDIR=;-DGNUPG_LIBEXECDIR=;-DGNUPG_LIBDIR=;-DGNUPG_DATADIR=;-DGNUPG_SYSCONFDIR=;-DGNUPG_LOCALSTATEDIR=;-DLOCALEDIR=" \
  make -j1 DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{etc/gnupg,var/cache/gnupg,home}
}
